Published on January 17, 2025

返回

点击转到题目

题目思路:

首先我们根据题意,可以很容易的想到,边界为0的特殊性。这直接导致了最后的均值都一定是0,因为边界是没有办法变化的。 所以我们接下来就是要讨论什么情况下不能让均值为0就可以了

代码实现:


#include <bits/stdc++.h>
using namespace std;

#define I_ERROR 0x3f3f3f3f
#define FastIOS {ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);}

const int N = 505;
int M[N][N];
int n;
int d[4][2] = { {1,0},{-1,0},{0,1},{0,-1} };

bool notSame(int x, int y) {
    return (x > 0 && y <= 0) || (x < 0 && y >= 0);
}
bool check(int ii, int jj) {
    bool t = true;
    for (int _ = 0; _ < 4; _++) {
        auto [dx, dy] = d[_];
        t &= (notSame(M[ii][jj], M[ii + dx][jj + dy]));
 
    }
    return t;
}
void solve() {
    cin >> n;
    n += 2;
    bool tar = true;
    for (int i = 1; i < n - 1; i++) {
        for (int j = 1; j < n - 1; j++) {
            cin >> M[i][j];
        }
    }

        for (int i = 1; i < n - 1; i++) {
            for (int j = 1; j < n - 1; j++) {
                if (M[i][j] != 0)
                    tar &= check(i, j);
            }
        }
    if (tar)cout << "YES" << endl;
    else cout << "NO" << endl;

}
int main() {
    FastIOS;

    solve();
    return 0;
}